/*
*  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
*/
package com.pitop.order.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.order.entity.Order;
import com.pitop.order.service.OrderDetailSV;
import com.pitop.order.service.OrderSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/order")
@Slf4j
@Api(value = "订单控制器", description = "订单控制器")
public class OrderCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private OrderSV orderSV;

    @Resource
    private OrderDetailSV orderDetailSV;

    /**
     * 查询Order一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Order一个详情信息", notes = "查询Order一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public Order load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Order order = orderSV.load(id, code);
        log.info(JSON.toJSONString(order));
        return order;
    }


    /**
     * 根据条件id查询Order一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Order一个详情信息", notes = "查询Order一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public Order loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Order order = orderSV.loadById(id);
        log.info(JSON.toJSONString(order));
        return order;
    }

    /**
     * 根据条件code查询Order一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Order一个详情信息", notes = "查询Order一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public Order loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Order order = orderSV.loadByCode(code);
        log.info(JSON.toJSONString(order));
        return order;
    }


    /**
     * 查询Order信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Order信息集合", notes = "查询Order信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单编号", paramType = "query"),
            @ApiImplicitParam(name = "itemSku", value = "产品sku", paramType = "query"),
            @ApiImplicitParam(name = "itemBoxNum", value = "产品箱数", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：已下单 Order，待发货 Pending_Delivery，已发货Shipped,取消 Cancel,延期 Delay", paramType = "query"),
            @ApiImplicitParam(name = "orderDateBegin", value = "期望出单日期", paramType = "query"),
            @ApiImplicitParam(name = "orderDateEnd", value = "期望出单日期", paramType = "query"),
            @ApiImplicitParam(name = "expectShipDateBegin", value = "期望出单日期", paramType = "query"),
            @ApiImplicitParam(name = "expectShipDateEnd", value = "期望出单日期", paramType = "query"),
            @ApiImplicitParam(name = "linkman", value = "客户名称", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Order> list(@RequestBody @ApiIgnore Order order, Integer curPage, Integer pageSize) {
        Page<Order> page = new Page<Order>(pageSize, curPage);
        List<Order> orders = orderSV.list(order, page.genRowStart(), page.getPageSize());
        int total = orderSV.count(order);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return orders;
    }


    /**
     * 查询Order信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Order信息集合", notes = "查询Order信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<Order> listByPk(Long id, String code, Integer curPage, Integer pageSize) {
        Page<Order> page = new Page<Order>(pageSize, curPage);
        List<Order> orders = orderSV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = orderSV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return orders;
    }

    /**
     * 统计Order信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计Order信息数量根据主键", notes = " 统计Order信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String code) {
        return orderSV.count(id, code);
    }

    /**
     * 统计Order信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Order信息数量", notes = "统计Order信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单编号", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编码", paramType = "query"),
            @ApiImplicitParam(name = "itemSku", value = "产品sku", paramType = "query"),
            @ApiImplicitParam(name = "itemSummary", value = "产品说明", paramType = "query"),
            @ApiImplicitParam(name = "itemTotalNum", value = "产品总量", paramType = "query"),
            @ApiImplicitParam(name = "itemBoxNum", value = "产品箱数", paramType = "query"),
            @ApiImplicitParam(name = "expectShipDate", value = "期望出单日期", paramType = "query"),
            @ApiImplicitParam(name = "orderDate", value = "下单日期", paramType = "query"),
            @ApiImplicitParam(name = "shipTime", value = "", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：已下单 Order，待发货 Pending_Delivery，已发货Shipped,取消 Cancel,延期 Delay", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody Order order) {
        return orderSV.count(order);
    }


    /**
     * 创建Order
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Order", notes = "创建Order")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单编号", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编码", paramType = "query"),
            @ApiImplicitParam(name = "itemSku", value = "产品sku", paramType = "query"),
            @ApiImplicitParam(name = "itemSummary", value = "产品说明", paramType = "query"),
            @ApiImplicitParam(name = "itemTotalNum", value = "产品总量", paramType = "query"),
            @ApiImplicitParam(name = "itemBoxNum", value = "产品箱数", paramType = "query"),
            @ApiImplicitParam(name = "expectShipDate", value = "期望出单日期", paramType = "query"),
            @ApiImplicitParam(name = "orderDate", value = "下单日期", paramType = "query"),
            @ApiImplicitParam(name = "shipTime", value = "", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：已下单 Order，待发货 Pending_Delivery，已发货Shipped,取消 Cancel,延期 Delay", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public Order build(@RequestBody @ApiIgnore Order order) {
        orderSV.save(order);
        return order;
    }


    /**
     * 修改Order
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Order", notes = "修改Order")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单编号", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编码", paramType = "query"),
            @ApiImplicitParam(name = "itemSku", value = "产品sku", paramType = "query"),
            @ApiImplicitParam(name = "itemSummary", value = "产品说明", paramType = "query"),
            @ApiImplicitParam(name = "itemTotalNum", value = "产品总量", paramType = "query"),
            @ApiImplicitParam(name = "itemBoxNum", value = "产品箱数", paramType = "query"),
            @ApiImplicitParam(name = "expectShipDate", value = "期望出单日期", paramType = "query"),
            @ApiImplicitParam(name = "orderDate", value = "下单日期", paramType = "query"),
            @ApiImplicitParam(name = "shipTime", value = "", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：已下单 Order，待发货 Pending_Delivery，已发货Shipped,取消 Cancel,延期 Delay", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public Order modify(@RequestBody @ApiIgnore Order order) {
        orderSV.modify(order);
        return order;
    }


    /**
     * 删除Order
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Order", notes = "删除Order")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(java.lang.Long id, java.lang.String code) {
        orderSV.delete(id, code);
        return BeanRet.create(true, "删除Order成功");
    }


}
